package br.ufpe.cin.analysis.plugin.charts;

import java.awt.Font;
import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;

import br.ufpe.cin.analysis.plugin.entities.Process;
import br.ufpe.cin.analysis.plugin.models.BarDataset;
import br.ufpe.cin.analysis.plugin.models.StatisticalBarDataset;

public class StatisticalBarChart extends Chart<StatisticalBarDataset> {
	
	
	private PlotOrientation orientation;
	private String axisA,axisB;


	public StatisticalBarChart(StatisticalBarDataset dataset, PlotOrientation orientation, String axisA, String axisB){

		this.orientation = orientation;
		this.axisA = axisA;
		this.axisB = axisB;

		this.setDataset(dataset);


	}

	public StatisticalBarChart(String xmlFileName, PlotOrientation orientation, String axisA, String axisB) throws JAXBException, IOException{

		this.orientation = orientation;
		this.axisA = axisA;
		this.axisB = axisB;

		this.setDataset( new StatisticalBarDataset(xmlFileName) );


	}

	public StatisticalBarChart(Process process, PlotOrientation orientation, String axisA, String axisB){

		this.orientation = orientation;
		this.axisA = axisA;
		this.axisB = axisB;

		this.setDataset(new StatisticalBarDataset(process));


	}

	@Override
	public void init() {
		
		CategoryItemRenderer renderer = new StatisticalBarRenderer();
		CategoryAxis xAxis = new CategoryAxis(this.axisA);
		ValueAxis yAxis = new NumberAxis(this.axisB);
		
		CategoryPlot plot = new CategoryPlot(this.getDataset().getJFreeChartDataset(), xAxis, yAxis, renderer);
		plot.setOrientation(this.orientation);
		JFreeChart chart = new JFreeChart(this.getDataset().getProcess().getName() ,new Font("Arial", Font.PLAIN,10),plot,true);
		
		this.setJChart( chart );

	}

	public PlotOrientation getOrientation() {
		return orientation;
	}

	public void setOrientation(PlotOrientation orientation) {
		this.orientation = orientation;
		this.getJChart().getCategoryPlot().setOrientation(this.orientation);
	}

	public String getAxisA() {
		return axisA;
	}

	public void setAxisA(String axisA) {
		this.axisA = axisA;
		this.getJChart().getCategoryPlot().getDomainAxis().setLabel(this.axisA);
	}

	public String getAxisB() {
		return axisB;
	}

	public void setAxisB(String axisB) {
		this.axisB = axisB;
		this.getJChart().getCategoryPlot().getRangeAxis().setLabel(this.axisB);
	}
	

}
